package cn.baiweigang.uu.db;

import cn.baiweigang.uu.enums.PeriodEnum;
import cn.baiweigang.uu.model.AccountInfo;
import cn.baiweigang.uu.model.DataMap;
import cn.baiweigang.uu.utils.DButil;
import cn.baiweigang.uu.utils.LogUtil;

import javax.xml.crypto.Data;
import java.util.Map;

public class SummarySheetDb extends BaseDb {

    public static void inster(AccountInfo accountInfo, PeriodEnum period, String dateTime, Map<String, String> param) {
        //如果不存在则插入
        if (queryByPeriodAndDateTime(accountInfo, period, dateTime) == null) {
            String sql = String.format("insert into summary_sheet (period,date_time,account_name,contract_code) values ('%s','%s','%s','%s')",
                    period.getCode(), dateTime, accountInfo.getAccountName(), accountInfo.getContractCode().getCode());
            DButil.update(sql);
        }
        //更新字段
        String sql = String.format("update summary_sheet set %s where period='%s' and date_time='%s' and account_name='%s' and contract_code='%s'",
                getUpdateSql(param), period.getCode(), dateTime, accountInfo.getAccountName(), accountInfo.getContractCode().getCode());
        DButil.update(sql);
    }

    public static DataMap queryByPeriodAndDateTime(AccountInfo accountInfo, PeriodEnum period, String dateTime) {
        String sql = String.format("select * from summary_sheet where period='%s' and date_time='%s' and account_name='%s' and contract_code='%s'",
                period.getCode(), dateTime, accountInfo.getAccountName(), accountInfo.getContractCode().getCode());
        return DButil.query(sql);
    }
}
